//
//  GYNewShopListVC.m
//  GYDemo
//
//  Created by Mrzhang on 2018/6/25.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYNewShopListVC.h"
#import "Head.h"
#import "GYNewShopCollectCell.h"
#import "GYNewChooseView.h"
#import "GYSearchBar.h"
#import "GYHomeBuyListVC.h"
#import "GYNewSaleDetailVC.h"
#import "GYCategoryViewModel.h"
@interface GYNewShopListVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UISearchBarDelegate>
{
    
        NSInteger currentPage;
    
}
@property (nonatomic,strong) UICollectionView *mCollection;
@property (nonatomic,strong) GYNewChooseView *headerView;
@property (nonatomic,strong) GYSearchBar * topSearchBar;
@property (nonatomic,strong) GYCategoryViewModel * likeProductInfoviewModel;
@property(nonatomic,strong)NSMutableDictionary* paraDataSource;
@end

@implementation GYNewShopListVC
#pragma mark - getter and setter
-(NSMutableDictionary *)paraDataSource{
    
    if (!_paraDataSource) {
        _paraDataSource=[[NSMutableDictionary alloc] init];
    }
    return _paraDataSource;
}
-(GYCategoryViewModel *)likeProductInfoviewModel
{
    if (!_likeProductInfoviewModel) {
        _likeProductInfoviewModel = [GYCategoryViewModel new];
    }
    return _likeProductInfoviewModel;
}
-(GYSearchBar *)topSearchBar
{
    if (!_topSearchBar) {
        _topSearchBar=[[GYSearchBar alloc] initWithFrame:CGRectMake(40, 0, self.view.width-90, 44)];
        _topSearchBar.placeholder=@"请输入关键字搜索容";
        _topSearchBar.delegate=self;
        _topSearchBar.tintColor = Main_Color_Black;
       // _topSearchBar.backgroundColor=HEXCOLOR(@"#F7F7F7");
        
    }
    return _topSearchBar;
}
-(GYNewChooseView *)headerView
{
    if (!_headerView) {
        _headerView=[[GYNewChooseView alloc] init];
        _headerView.backgroundColor=[UIColor whiteColor];
        _headerView.x=0;
        _headerView.y=0;
        _headerView.width=ScreenWidth;
        _headerView.height=WFCGFloatY(40);
        @WeakSelf;
        _headerView.backHandler = ^(NSInteger type, NSDictionary *backDic) {
            if (type==1)
            {
                for (NSString* key in backDic.allKeys) {
                    [selfp.paraDataSource setObject:[backDic objectForKey:key] forKey:key];
                }
            }
            if (type==2)
            {
                selfp.paraDataSource[@"sort"]=[backDic objectForKey:@"sort"];
            }
            
            if (type==3)
            {
               
                selfp.paraDataSource[@"minprice"]=[backDic objectForKey:@"minprice"];
                selfp.paraDataSource[@"maxprice"]=[backDic objectForKey:@"maxprice"];
            }
            
            [selfp reloadData];
        };


        [_headerView reqeustData:_typeID?_typeID:_passID viewType:_typeID?1:2];
    }
    return _headerView;
}
-(UICollectionView *)mCollection
{
    if (!_mCollection)
    {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        CGFloat size = (ScreenWidth-21)/2;
        
        [flowLayout setItemSize:CGSizeMake(size, size + WFCGFloatY(94))];//设置cell的尺寸
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];//设置其布局方向
        flowLayout.sectionInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);//设置其边界
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 7;
        
        _mCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(7, WFCGFloatY(40), ScreenWidth-14, self.view.height - GY_Nav_height-WFCGFloatY(40)-self.bottomShelterHeight) collectionViewLayout:flowLayout];
        
        _mCollection.dataSource = self;
        _mCollection.delegate = self;
        [_mCollection setBackgroundColor:HEXCOLOR(@"#F1F1F2")];
        _mCollection.showsVerticalScrollIndicator=NO;
        _mCollection.showsHorizontalScrollIndicator=NO;
        //注册Cell，必须要有
        [_mCollection registerClass:[GYNewShopCollectCell class] forCellWithReuseIdentifier:@"colcell"];
    
        @WeakSelf;
        self.mCollection.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            currentPage = 1;
            [selfp setOriginData];
        }];
        self.mCollection.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
            if (selfp.likeProductInfoviewModel.canAddMore)
            {
                currentPage += 1;
                [selfp.paraDataSource setObject:[NSString stringWithFormat:@"%ld",currentPage] forKey:@"page"];
                [selfp reloadData];
                [selfp.mCollection.mj_footer endRefreshing];
            }else
            {
                [selfp.mCollection.mj_footer endRefreshingWithNoMoreData];
            }
        }];
    }
    return _mCollection;
}
#pragma mark - event
-(void)viewModelBand
{
    @WeakSelf;
    GCBaseObservationModel *observation = [GCBaseObservationModel new];
    observation.observation = self.likeProductInfoviewModel;
    observation.keyPath = @"datas";
    observation.handler = ^(NSString *keyPath) {
        [WFFunctions hideNoDataLabel:selfp.mCollection];
        [selfp reloadView];
        if ([selfp.paraDataSource[@"page"] isEqualToString:@"1"]) {
            [selfp.mCollection reloadData];
            [selfp.mCollection.mj_header endRefreshing];
             [selfp.mCollection.mj_footer endRefreshing];
        }
        if (self.likeProductInfoviewModel.datas.count==0) {
            [WFFunctions showNodataLabel:@"暂无数据" inView:selfp.mCollection];
        }
        
    };
    [self registObservation:observation];
}
-(void)setOriginData
{
    currentPage = 1;
    
//    for (NSString* key in self.paraDataSource.allKeys) {
//        if (![key isEqualToString:@"sort"])
//        {
//            [self.paraDataSource removeObjectForKey:key];
//        }
//    }
    if (![WFFunctions WFStrCheckEmpty:_passID]) {
        self.paraDataSource[@"protypeid"]=[NSString stringWithFormat:@"%@",_passID];
    }
    if (![WFFunctions WFStrCheckEmpty:_typeID]) {
        self.paraDataSource[@"topid"]=[NSString stringWithFormat:@"%@",_typeID];
    }
    self.paraDataSource[@"page"]=@"1";
    self.paraDataSource[@"pagesize"]=@"10";
    [self reloadData];
}
-(void)reloadView
{
    [self.mCollection reloadData];
}
-(void)reloadData
{
    [self.likeProductInfoviewModel requestDatas:self.paraDataSource];
    
}
#pragma mark - life

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavgationTheme:1];
   // [self loadTitleV];
    [self.navigationController.navigationBar addSubview:self.topSearchBar];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"list_memu"] style:UIBarButtonItemStylePlain target:self action:@selector(gotoMenMu)];
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.mCollection];
    [self viewModelBand];
     [self setOriginData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - delegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.likeProductInfoviewModel.datas.count;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GYNewShopCollectCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"colcell" forIndexPath:indexPath];
    cell.backgroundColor=[UIColor whiteColor];
    

    [cell setSourceModel:[self.likeProductInfoviewModel.datas objectAtIndex:indexPath.row]];
    return cell;
}
#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GYNewGoodListModel *model = self.likeProductInfoviewModel.datas[indexPath.row];
    GYNewSaleDetailVC* new=[[GYNewSaleDetailVC alloc] init];
    new.passID=model.id;
    [self.navigationController pushViewController:new animated:YES];
   
}

#pragma mark - delegate
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    
    [_topSearchBar resignFirstResponder];
    self.paraDataSource[@"keyword"]=@"";
    _topSearchBar.text=@"";
    [_topSearchBar setShowsCancelButton:NO animated:YES];
     [self reloadData];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [_topSearchBar resignFirstResponder];
 
    [_topSearchBar setShowsCancelButton:YES animated:YES];
   
    self.paraDataSource[@"keyword"]=_topSearchBar.text;
    [self reloadData];
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
